package jmine.tec.utils.reflection.fast.asm;

import java.security.SecureClassLoader;

/**
 * @author takeshi
 */
public class OpenClassLoader extends SecureClassLoader {

    /**
     */
    public OpenClassLoader() {
        this(selectClassloader());
    }

    /**
     * @param parent {@link ClassLoader}
     */
    public OpenClassLoader(ClassLoader parent) {
        super(parent);
    }

    /**
     * @param name nome da classe
     * @param b o array de bytes com o bytecode da classe
     * @return {@link Class}
     */
    public Class<?> defineClass(String name, byte[] b) {
        return super.defineClass(name, b, 0, b.length);
    }

    /**
     * Seleciona um classloader. Tenta primeiro o context classloader
     * 
     * @return {@link ClassLoader}
     */
    private static ClassLoader selectClassloader() {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader != null) {
            return loader;
        }
        return CompiledFastMethod.class.getClassLoader();
    }

}
